#include "settinglib.h"
#include <QDebug>
SettingLib* SettingLib::settinglib=nullptr;

SettingLib *SettingLib::getInstance()
{
    if(settinglib==nullptr)
    {
        settinglib=new SettingLib;

    }
    return settinglib;
}
bool SettingLib::isFirst()
{
    return settingshash.isEmpty();
}

void SettingLib::initSettings()
{
    //settings=new QSettings("./OneChat.occ");
    QFile file("./OneChat.occ");
    file.open(QIODevice::ReadOnly);
    settings=new QSettings("./OneChat.occ", QSettings::IniFormat);

    if(!file.exists())
    {
        file.open(QIODevice::WriteOnly);
        file.close();
        settings->setValue("user","");
        settings->setValue("password","");
        settings->setValue("remember",false);
        settings->setValue("autologon",false);
        settings->setValue("newmessagering",true);
        settings->setValue("voicecallring",true);
        settings->setValue("videocallring",true);
        settings->setValue("enter",true);
        settings->setValue("ring",true);
        settings->sync();
    }
    settingshash.insert(USER,settings->value("user"));
    settingshash.insert(PASSWORD,settings->value("password"));
    settingshash.insert(REMEMBER,settings->value("remember"));
    settingshash.insert(AUTOLOGON,settings->value("autologon"));
    settingshash.insert(NEWMESSAGERING,settings->value("newmessagering"));
    settingshash.insert(VOICECALLRING,settings->value("voicecallring"));
    settingshash.insert(VIDEOCALLRING,settings->value("videocallring"));
    settingshash.insert(ENTER,settings->value("enter"));
    settingshash.insert(RING,settings->value("ring"));
    emit s_loadDone();
}

void SettingLib::setValue(Type key, QVariant val)
{
    QString type;
    bool valbool=val.toBool();
    switch(key)
    {
    case USER:
        type="user";

        break;
    case PASSWORD:
        type="password";
        break;
    case REMEMBER:
        type="remember";
        break;
    case AUTOLOGON:
        type="autologon";
        break;
    case NEWMESSAGERING:
        type="newmessagering";
        emit s_newmessagering(valbool);
        break;
    case VOICECALLRING:
        type="voicecallring";
        emit s_voicecallring(valbool);
        break;
    case VIDEOCALLRING:
        type="videocallring";
        emit s_videocallring(valbool);
        break;
    case ENTER:
        type="enter";
        emit s_enter(valbool);
        break;
    case RING:
        type="ring";
        emit s_ring(val.toInt());
        break;
    }

    settingshash[key]=val;
    settings->setValue(type,val);
    settings->sync();
}

QVariant SettingLib::getValue(Type key)
{
    return settingshash[key];
}

SettingLib::SettingLib()
{}
